import YZRequest from "./request";
import {
  getStorage
} from '../utils/storage.js'

const http = new YZRequest('http://124.93.196.45:10001')
const loginUrl = '/dev-api/login'

//配置响应拦截器
http.interceptors.request = config => {
  //如果是登录请求，不需要带token，带了缓存的token反而会出问题
  if (!config.url.endsWith(loginUrl)) {
    const token = getStorage('token')
    //如果存在token 
    if (token) {
      config.header = {
        'Authorization': 'Bearer ' + token
      }
    }
  }
  //打印请求信息
  console.log("http request: ", config);
}
http.interceptors.response = response => {
  const {
    isSuccess,
    data
  } = response
  //打印响应信息
  console.log("http response: ", response);
  if (!isSuccess) {
    //网络超时或异常
    uni.showToast({
      title: '网络异常，请检查网络'
    })
    return data
  }
  //服务器有反馈
  if (data.code === 200) {
    //请求成功
    return data
  } else {
    uni.showToast({
      title: data.msg
    })
    return null
  }
}
export default http